package convert

import (
	"errors"
	"mgs/global"
	"os"
)

func fileIsExist(path string) bool {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return false
	}
	return true
}

func exporterMap() (m map[string]interface{}) {
	m = map[string]interface{}{
		".doc":  new(Word),
		".docx": new(Word),
		".xls":  new(Excel),
		".xlsx": new(Excel),
		".ppt":  new(PowerPoint),
		".pptx": new(PowerPoint),
	}
	return
}

func Export(fileExt, inFile, outDir string) (outFile string, err error) {
	libreOffice := global.CONFIG.System.LibreOffice
	if len(libreOffice) > 0 {
		return libreOfficeToPdf(libreOffice, inFile, outDir)
	}

	// windows exec office or wps
	if fileIsExist(inFile) && fileIsExist(outDir) {
		exporter := exporterMap()[fileExt]
		if _, ok := exporter.(Exporter); ok {
			return exporter.(Exporter).Export(inFile, outDir)
		}
	}
	return outFile, errors.New("不支持转换文件类型")
}
